package com.bci.pwtz.controller;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.util.Streams;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import com.bci.pwtz.common.PwtzConstants;
import com.bci.pwtz.common.util.JsonUtils;
import com.bci.pwtz.common.util.PwtzUtils;
import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.exception.PwtzException;
import com.bci.pwtz.mysql.dao.ConfigMapper;
import com.bci.pwtz.mysql.model.Company;
import com.bci.pwtz.mysql.model.CompanyUser;
import com.bci.pwtz.mysql.model.Config;
import com.bci.pwtz.mysql.model.ImportTemplate;
import com.bci.pwtz.mysql.model.Vdystat;
import com.bci.pwtz.mysql.model.Vzdy;
import com.bci.pwtz.mysql.model.Vzystat;
import com.bci.pwtz.service.CompanyService;
import com.bci.pwtz.service.ExcelUtilService;
import com.bci.pwtz.service.VdystatService;
import com.bci.pwtz.service.VzdyService;
import com.bci.pwtz.service.VzystatService;

@Controller
@RequestMapping("/user/excel")
public class ExcelUtilController  extends AbstractController{
	@Autowired
    private ExcelUtilService excelUtilService;
	@Autowired
	private ConfigMapper configMapper;
	@Autowired
    private VzdyService vzdyService;
	
	@Autowired
    private VdystatService vdystatService;
	
	@Autowired
    private VzystatService vzystatService;
	
	@Autowired
    private CompanyService companyService;

	
	@RequestMapping(value="/export",method=RequestMethod.POST)
    @ResponseBody
    public Map<String, String> export(int exportType,long templateId,String stratTime, String endTime){
        Map<String, String> map=new HashMap<String, String>();
        if(templateId == 2){//导出GIS
            map=GISExel(exportType,templateId,stratTime,endTime);
        }else if(templateId ==4){//导出营销坐标点GIS
            map=YXGISExel(templateId);
        }
        return map;
    }

    public Map<String,String> YXGISExel(long templateId){
        Map<String, String> map=new HashMap<String, String>();
        try{
            CompanyUser companyUser = (CompanyUser)session.getAttribute("user");
            String ret = excelUtilService.exportYXExcle(companyUser.getLastCompanyId(), templateId);
            if(ret.equals("2")){
                map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL,ERROR_VALUE_MSG_TYPE_ERROR,null,"导出失败");
            }else{
                ret = ret.substring(ret.indexOf("Atta"),ret.length());
                map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS,ERROR_VALUE_MSG_TYPE_INFO,null,ret);
            }
        }catch (PwtzDatabaseException e){
            map = packageException(e);
        }
        return map;
    }

    public Map<String, String>  GISExel(int exportType,long templateId,String stratTime, String endTime){
        Map<String, String> map=new HashMap<String, String>();
        try
        {
            CompanyUser companyUser = (CompanyUser)session.getAttribute("user");
            //String path = request.getSession().getServletContext().getRealPath("/");
//          path = path.replaceAll("\\\\", "/");
            if(exportType==0){
                String ret = excelUtilService.exportExcle(companyUser.getLastCompanyId(), templateId, stratTime, endTime);
                if("2".equals(ret)){
                    map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL,ERROR_VALUE_MSG_TYPE_ERROR,null,"导出失败");
                }else{
                    ret = excelUtilService.exportPic(companyUser.getLastCompanyId(), stratTime, endTime, ret,exportType);
                    if("2".equals(ret)){
                        map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL,ERROR_VALUE_MSG_TYPE_ERROR,null,"导出失败");
                    }else{
                        ret = ret.substring(ret.indexOf("Atta"),ret.length());
                        map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS,ERROR_VALUE_MSG_TYPE_INFO,null,ret);
                    }
                }
            }else if(exportType==1){
                String ret = excelUtilService.exportExcle(companyUser.getLastCompanyId(), templateId, stratTime, endTime);
                if("2".equals(ret)){
                    map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL,ERROR_VALUE_MSG_TYPE_ERROR,null,"导出失败");
                }else{
                    String fileName="";
                    List<Config> exportExcelNameList = configMapper
                            .selectByKey("ExportExcelName");
                    if (exportExcelNameList != null
                            && exportExcelNameList.size() > 0) {
                        fileName = exportExcelNameList.get(0).getConfigValue();
                    }
                    ret = ret.substring(ret.indexOf("Atta"),ret.length());
                    map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS,ERROR_VALUE_MSG_TYPE_INFO,null,ret+fileName);
                }
            }else if(exportType==2){
                String dir="";
                List<Config> exportDataPathList = configMapper
                        .selectByKey("ExportDataPath");
                if (exportDataPathList != null && exportDataPathList.size() > 0) {
                    dir = exportDataPathList.get(0).getConfigValue();
                    dir = dir.replace("@COMPANYID", String.valueOf(companyUser.getLastCompanyId()));
                    dir = dir.replace("@TIMESTAMP", String.valueOf(System
                            .currentTimeMillis()));
                }
                String ret = excelUtilService.exportPic(companyUser.getLastCompanyId(), stratTime, endTime, dir,exportType);
                if("2".equals(ret)){
                    map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL,ERROR_VALUE_MSG_TYPE_ERROR,null,"导出失败");
                }else if("1".equals(ret)){
                    map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL,ERROR_VALUE_MSG_TYPE_ERROR,null,"none");
                }else{
                    ret = ret.substring(ret.indexOf("Atta"),ret.length());
                    map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS,ERROR_VALUE_MSG_TYPE_INFO,null,ret);
                }
            }else{
                String dir="";
                List<Config> exportDataPathList = configMapper
                        .selectByKey("ExportDataPath");
                if (exportDataPathList != null && exportDataPathList.size() > 0) {
                    dir = exportDataPathList.get(0).getConfigValue();
                    dir = dir.replace("@COMPANYID", String.valueOf(companyUser.getLastCompanyId()));
                    dir = dir.replace("@TIMESTAMP", String.valueOf(System
                            .currentTimeMillis()));
                }
                String ret = excelUtilService.exportGISPic(companyUser.getLastCompanyId(), stratTime, endTime, dir,exportType);
                if("2".equals(ret)){
                    map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL,ERROR_VALUE_MSG_TYPE_ERROR,null,"导出失败");
                }else if("1".equals(ret)){
                    map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL,ERROR_VALUE_MSG_TYPE_ERROR,null,"none");
                }else{
                    ret = ret.substring(ret.indexOf("Atta"),ret.length());
                    map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS,ERROR_VALUE_MSG_TYPE_INFO,null,ret);
                }
            }
        }
        catch (PwtzDatabaseException e)
        {
            map = packageException(e);
        }
        return map;
    }

    @RequestMapping(value="/exportZYPMS",method=RequestMethod.POST)
    @ResponseBody
    public Map<String, String> exportZYPMS(String devicename,long templateId,String deviceId){
        Map<String, String> map=new HashMap<String, String>();
        CompanyUser companyUser = (CompanyUser)session.getAttribute("user");
        try{
            String ret=excelUtilService.ZYPMSExel(companyUser.getLastCompanyId(),templateId,deviceId,devicename);
            if("2".equals(ret)){
                map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL,ERROR_VALUE_MSG_TYPE_ERROR,null,"导出失败");
            }else{
                ret = ret.substring(ret.indexOf("Atta"),ret.length());
                ret=ret.replaceAll("#","jinjinjin");
                map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS,ERROR_VALUE_MSG_TYPE_INFO,null,ret);
            }
        }catch (Exception e){
            map = packageException(e);
        }
        return map;
    }


    @RequestMapping(value="/exportDYPMS",method=RequestMethod.POST)
    @ResponseBody
    public Map<String, String> exportDYPMS(String devicename,long templateId,String deviceId){
        Map<String, String> map=new HashMap<String, String>();
        CompanyUser companyUser = (CompanyUser)session.getAttribute("user");
        try{
            String ret=excelUtilService.DYPMSExel(companyUser.getLastCompanyId(),templateId,deviceId,devicename);
            if("2".equals(ret)){
                map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL,ERROR_VALUE_MSG_TYPE_ERROR,null,"导出失败");
            }else{
                ret = ret.substring(ret.indexOf("Atta"),ret.length());
                ret=ret.replaceAll("#","jinjinjin");
                map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS,ERROR_VALUE_MSG_TYPE_INFO,null,ret);
            }
        }catch (Exception e){
            map = packageException(e);
        }
        return map;
    }
    @RequestMapping(value="/exportAllDYPMS",method=RequestMethod.POST)
    @ResponseBody
    public Map<String, String> exportAllDYPMS(String devicename,long templateId,String deviceId,int deviceType,String transformerCode){
        Map<String, String> map=new HashMap<String, String>();
        CompanyUser companyUser = (CompanyUser)session.getAttribute("user");
        try{
            String ret=excelUtilService.DYAllPMSExel(companyUser.getLastCompanyId(),templateId,deviceId,devicename,deviceType,transformerCode);
            if("2".equals(ret)){
                map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL,ERROR_VALUE_MSG_TYPE_ERROR,null,"导出失败");
            }else{
                ret = ret.substring(ret.indexOf("Atta"),ret.length());
                ret=ret.replaceAll("#","jinjinjin");
                map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS,ERROR_VALUE_MSG_TYPE_INFO,null,ret);
            }
        }catch (Exception e){
            map = packageException(e);
        }
        return map;
    }
    @RequestMapping(value={"download"},method={RequestMethod.GET})
    public void download(HttpServletResponse response,
                         @RequestParam String filename){
        _log.info(filename);
        filename=filename.replaceAll("jinjinjin","#");
        String basePath=configMapper.selectByKey("BasePath").get(0).getConfigValue();
        File file =  new File(basePath+filename);
//        File file =  new File("E:/pwtz/apache-tomcat-7.0.79/webapps/pwtz/"+filename);
        if(file.exists()){
            response.setContentType("application/force-download");
            String ret=filename.substring(filename.lastIndexOf("/")+1);
            String fileName= null; //解决中文乱码问题
            try {
                fileName = new String(ret.getBytes("utf-8"),"iso-8859-1");
                response.addHeader("Content-Disposition", "attachment;filename="+fileName);
                FileInputStream fileInputStream = new FileInputStream(file);
                byte[] by  = new byte[fileInputStream.available()];
                fileInputStream.read(by);
                OutputStream outputStream = response.getOutputStream();
                outputStream.write(by);
                fileInputStream.close();
                outputStream.close();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }



	@RequestMapping(value="/importlist",method=RequestMethod.POST)
    @ResponseBody
    public List<ImportTemplate> importlist(int deviceTypeId) throws PwtzException{
        
        List<ImportTemplate> list = new ArrayList<ImportTemplate>();
        try
        {
            list = excelUtilService.selectTemplateByDeviceTypeId(deviceTypeId);
        }
        catch (PwtzDatabaseException e)
        {
            e.printStackTrace();
        }
        
        return list;
    }
	
	@RequestMapping("/readfile")
    @ResponseBody
    public void readFile(HttpServletRequest request,HttpServletResponse response,MultipartFile userFile)
    {
	    List list=new ArrayList();
		String result = "";
        String url = "";
        String dir = "";
		CompanyUser companyUser = (CompanyUser)session.getAttribute("user");
		long templateId=Long.parseLong(request.getParameter("templateId"));
//		String path = request.getSession().getServletContext().getRealPath("/");
//      path = path.replaceAll("\\\\", "/");
//		List<Config> exportDataPathList = configMapper.selectByKey("ImportDataPath");
		if(PwtzConstants.THE_SERVER_TYPE==1){
		    dir=request.getSession().getServletContext().getRealPath("/")+"\\upload\\";
        }else{
            dir=request.getSession().getServletContext().getRealPath("/")+"/upload/";
            dir = dir.replaceAll("\\\\", "/");    //本地
        }
		File fdir = new File(dir);
		if (!fdir.exists()) {
			fdir.mkdirs();
		}
		try
        {
            if (_log.isInfoEnabled())
            {
                _log.info("开始上传文件");
            }
            String prefix = "";
            String fileName = "";
            String newFileName = "";

            fileName = userFile.getOriginalFilename();
            prefix = fileName.substring(fileName.lastIndexOf(".")).toLowerCase();
            newFileName = System.currentTimeMillis() + PwtzUtils.getRandomNumber(4)+ prefix;
            url = dir + newFileName;
            BufferedInputStream in = new BufferedInputStream(userFile.getInputStream());
            File file = new File(dir);
            if  (!file .exists()  && !file .isDirectory())      
            {       
                file .mkdir();    
            }
 
            FileOutputStream out = new FileOutputStream(new File(url));
            BufferedOutputStream output = new BufferedOutputStream(out);
            Streams.copy(in, output, true);
            
            _log.info("url="+url);
            result = url;
            if(result.length()>17){
                list=importexcle(templateId,result);
            }
        }
        catch (IOException e)
        {
            result = "文件上传IO异常";
            _log.error(ExceptionUtils.getStackTrace(e));
        }
        catch (Exception e)
        {
            result = "未知异常";
            _log.error(ExceptionUtils.getStackTrace(e));
        }
        writeJsonObject(request,response,"{\"list\":" + JsonUtils.bean2json(list) + "}");
    }
	public List importexcle(long templateId, String templateUrl){
        List returnList = new ArrayList();
        CompanyUser companyUser = (CompanyUser)session.getAttribute("user");
            Map excelmap = new HashMap();
            //templateUrl = templateUrl.replace("\\", "\\\\");
            if (templateUrl.substring(templateUrl.lastIndexOf(".")).toLowerCase().endsWith(".xls"))
            {
                excelmap = excelUtilService.import2003Excle(companyUser.getLastCompanyId(), templateId, templateUrl, companyUser.getCompanyUserId());
            }
            else if (templateUrl.substring(templateUrl.lastIndexOf(".")).toLowerCase().endsWith(".xlsx"))
            {
                excelmap = excelUtilService.import2007Excle(companyUser.getLastCompanyId(), templateId, templateUrl, companyUser.getCompanyUserId());
            }
            returnList.add(excelmap.get("success"));
            returnList.add(excelmap.get("insert"));
            returnList.add(excelmap.get("update"));
            returnList.add((List)excelmap.get("failure"));

        return returnList;
    }
	/*@RequestMapping(value="/importexcle",method=RequestMethod.POST)
    @ResponseBody
    public List importexcle(long templateId, String templateUrl){
        List returnList = new ArrayList();
        CompanyUser companyUser = (CompanyUser)session.getAttribute("user");
        	Map excelmap = new HashMap();
        	//templateUrl = templateUrl.replace("\\", "\\\\");
        	if (templateUrl.substring(templateUrl.lastIndexOf(".")).toLowerCase().endsWith(".xls"))
            {
        		excelmap = excelUtilService.import2003Excle(companyUser.getLastCompanyId(), templateId, templateUrl, companyUser.getCompanyUserId());
            }
            else if (templateUrl.substring(templateUrl.lastIndexOf(".")).toLowerCase().endsWith(".xlsx"))
            {
            	excelmap = excelUtilService.import2007Excle(companyUser.getLastCompanyId(), templateId, templateUrl, companyUser.getCompanyUserId());
            }
        	returnList.add(excelmap.get("success"));
        	returnList.add(excelmap.get("insert"));
        	returnList.add(excelmap.get("update"));
        	returnList.add((List)excelmap.get("failure"));

        return returnList;
    }*/
	
	private void writeJsonObject(HttpServletRequest request, HttpServletResponse resp, String jsonData)
    {
        resp.setContentType("text/html;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        PrintWriter pw = null;
        try
        {
            pw = resp.getWriter();
            pw.print(jsonData);
            pw.flush();
        }
        catch (IOException e)
        {
            handleException(e);
        }
        finally
        {
            if (null != pw)
            {
                pw.close();
            }
        }
    }
	
	@RequestMapping(value="/listuser",method=RequestMethod.POST)
    @ResponseBody
    public Map<String, String>  listuser(Long companyId,Long operatorId,String beginTime,String endTime,String levelname){
        List<Vzdy> list = new ArrayList<Vzdy>();
        Map<String, String> map=new HashMap<String, String>();
        String ret = "";
        try
        {
        	CompanyUser companyUser = (CompanyUser)session.getAttribute("user");
        	//String path = request.getSession().getServletContext().getRealPath("/");
//            path = path.replaceAll("\\\\", "/");
            if(companyId==0){
                list=vzdyService.findListByUserId(getLastCompanyId(), 1, operatorId, beginTime, endTime,levelname);
                if(list!=null&&list.size()>0){
                    ret = excelUtilService.exportVzdyUserList(list, companyUser.getLastCompanyId());
                    if("2".equals(ret)){
                        map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL,ERROR_VALUE_MSG_TYPE_ERROR,null,"导出失败");    
                    }else{
                        ret = ret.substring(ret.indexOf("A"),ret.length());
                        map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS,ERROR_VALUE_MSG_TYPE_INFO,null,ret);
                    }
                }else{
                    map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL,ERROR_VALUE_MSG_TYPE_ERROR,null,"没有可以导出的数据");
                }
            }else{
                list=vzdyService.findListByUserId(companyId,0, operatorId, beginTime, endTime,levelname);
                if(list!=null&&list.size()>0){
                    ret = excelUtilService.exportVzdyUserList(list, companyId);
                    if("2".equals(ret)){
                        map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL,ERROR_VALUE_MSG_TYPE_ERROR,null,"导出失败");    
                    }else{
                        ret = ret.substring(ret.indexOf("A"),ret.length());
                        map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS,ERROR_VALUE_MSG_TYPE_INFO,null,ret);
                    }
                }else{
                    map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL,ERROR_VALUE_MSG_TYPE_ERROR,null,"没有可以导出的数据");
                }
            }
        }
        catch (PwtzDatabaseException e)
        {
            e.printStackTrace();
        }
        return map;
    }
	
	@RequestMapping(value="/vzdylist",method=RequestMethod.POST)
    @ResponseBody
    public Map<String, String>  vzdylist(Long companyId,String levelname){
        List<Vzdy> list = new ArrayList<Vzdy>();
        Map<String, String> map=new HashMap<String, String>();
        String ret = "";
        try
        {
        	CompanyUser companyUser = (CompanyUser)session.getAttribute("user");
        	//String path = request.getSession().getServletContext().getRealPath("/");
//        	path = path.replaceAll("\\\\", "/");
            if(companyId==0){
                list= vzdyService.findList(getLastCompanyId(),1,levelname);
                if(list!=null&&list.size()>0){
                    ret = excelUtilService.exportVzdyList(list, companyUser.getLastCompanyId());
                    if("2".equals(ret)){
                        map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL,ERROR_VALUE_MSG_TYPE_ERROR,null,"导出失败");    
                    }else{
                        ret = ret.substring(ret.indexOf("A"),ret.length());
                        map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS,ERROR_VALUE_MSG_TYPE_INFO,null,ret);
                    }
                }else{
                    map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL,ERROR_VALUE_MSG_TYPE_ERROR,null,"没有可以导出的数据");
                }
            }else{
                list= vzdyService.findList(companyId,0,levelname);
                if(list!=null&&list.size()>0){
                    ret = excelUtilService.exportVzdyList(list, companyId);
                    if("2".equals(ret)){
                        map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL,ERROR_VALUE_MSG_TYPE_ERROR,null,"导出失败");    
                    }else{
                        ret = ret.substring(ret.indexOf("A"),ret.length());
                        map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS,ERROR_VALUE_MSG_TYPE_INFO,null,ret);
                    }
                }else{
                    map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL,ERROR_VALUE_MSG_TYPE_ERROR,null,"没有可以导出的数据");
                }
            }
        }
        catch (PwtzDatabaseException e)
        {
            e.printStackTrace();
        }
        return map;
    }
	
	@RequestMapping(value="/vdystatlist",method=RequestMethod.POST)
    @ResponseBody
    public Map<String, String>  vdystatlist(Long companyId){
        List<Vdystat> list = new ArrayList<Vdystat>();
        Map<String, String> map=new HashMap<String, String>();
        String ret = "";
        try
        {
            CompanyUser companyUser = (CompanyUser)session.getAttribute("user");
            String path = "";
            if(PwtzConstants.THE_SERVER_TYPE==1){
                path=request.getSession().getServletContext().getRealPath("/")+"\\upload\\";
            }else{
                path=request.getSession().getServletContext().getRealPath("/")+"/upload/";
                path = path.replaceAll("\\\\", "/");    //本地
            }
            //path = path.replaceAll("/", "\\\\");    //服务器
            String companyName="";
            if(companyId==0){
                list= vdystatService.findListByUserId(getLastCompanyId(),1);
                if(list!=null&&list.size()>0){
                    Company company =companyService.load(getLastCompanyId());
                    if(company!=null){
                        companyName=company.getCompanyName();
                    }
                    ret = excelUtilService.exportVdystatList(list, companyName,path,true);
                    if("2".equals(ret)){
                        map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL,ERROR_VALUE_MSG_TYPE_ERROR,null,"导出失败");    
                    }else{
                        ret = ret.substring(ret.indexOf("upload"),ret.length());
                        map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS,ERROR_VALUE_MSG_TYPE_INFO,null,ret);
                    }
                }else{
                    map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL,ERROR_VALUE_MSG_TYPE_ERROR,null,"没有可以导出的数据");
                }
            }else{
                list= vdystatService.findListByUserId(companyId,0);
                if(list!=null&&list.size()>0){
                    Company company =companyService.load(companyId);
                    if(company!=null){
                        companyName=company.getCompanyName();
                    }
                    ret = excelUtilService.exportVdystatList(list, companyName,path,false);
                    if("2".equals(ret)){
                        map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL,ERROR_VALUE_MSG_TYPE_ERROR,null,"导出失败");    
                    }else{
                        ret = ret.substring(ret.indexOf("upload"),ret.length());
                        map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS,ERROR_VALUE_MSG_TYPE_INFO,null,ret);
                    }
                }else{
                    map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL,ERROR_VALUE_MSG_TYPE_ERROR,null,"没有可以导出的数据");
                }
            }
        }
        catch (PwtzDatabaseException e)
        {
            e.printStackTrace();
        }
        return map;
    }
	
	@RequestMapping(value="/vzystatlist",method=RequestMethod.POST)
    @ResponseBody
    public Map<String, String>  vzystatlist(Long companyId){
        List<Vzystat> list = new ArrayList<Vzystat>();
        Map<String, String> map=new HashMap<String, String>();
        String ret = "";
        try
        {
            CompanyUser companyUser = (CompanyUser)session.getAttribute("user");
            String path = "";
            if(PwtzConstants.THE_SERVER_TYPE==1){
                path=request.getSession().getServletContext().getRealPath("/")+"\\upload\\";
            }else{
                path=request.getSession().getServletContext().getRealPath("/")+"/upload/";
                path = path.replaceAll("\\\\", "/");    //本地
            }
            //path = path.replaceAll("/", "\\\\");    //服务器
            String companyName="";
            if(companyId==0){
                list= vzystatService.findListByUserId(getLastCompanyId(),1);
                if(list!=null&&list.size()>0){
                    Company company =companyService.load(getLastCompanyId());
                    if(company!=null){
                        companyName=company.getCompanyName();
                    }
                    ret = excelUtilService.exportVzystatList(list, companyName,path,true);
                    if("2".equals(ret)){
                        map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL,ERROR_VALUE_MSG_TYPE_ERROR,null,"导出失败");    
                    }else{
                        ret = ret.substring(ret.indexOf("upload"),ret.length());
                        map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS,ERROR_VALUE_MSG_TYPE_INFO,null,ret);
                    }
                }else{
                    map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL,ERROR_VALUE_MSG_TYPE_ERROR,null,"没有可以导出的数据");
                }
            }else{
                list= vzystatService.findListByUserId(companyId,0);
                if(list!=null&&list.size()>0){
                    Company company =companyService.load(companyId);
                    if(company!=null){
                        companyName=company.getCompanyName();
                    }
                    ret = excelUtilService.exportVzystatList(list, companyName,path,false);
                    if("2".equals(ret)){
                        map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL,ERROR_VALUE_MSG_TYPE_ERROR,null,"导出失败");    
                    }else{
                        ret = ret.substring(ret.indexOf("upload"),ret.length());
                        map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS,ERROR_VALUE_MSG_TYPE_INFO,null,ret);
                    }
                }else{
                    map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL,ERROR_VALUE_MSG_TYPE_ERROR,null,"没有可以导出的数据");
                }
            }
        }
        catch (PwtzDatabaseException e)
        {
            e.printStackTrace();
        }
        return map;
    }
}
